package com.hxalex.simulationhandle.util;

import com.alibaba.fastjson.JSON;
import com.hxalex.simulationhandle.common.ResultModel;
import com.hxalex.simulationhandle.common.XTParamCommom;
import com.thinkdit.lib.base.IRequestCallback;
import com.thinkdit.lib.util.HttpUtils;
import com.thinkdit.lib.util.L;
import com.thinkdit.lib.util.StringUtil;

import java.util.Map;
import java.util.Random;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by qiuda on 16/6/7.
 */
public class XTHttpUtil extends HttpUtils<ResultModel> {
    private static final String TAG = "HttpUtils";

    private static XTHttpUtil XT_HTTP;
    private Random mRandom;
    public static final MediaType MediaType_JSON = MediaType.parse("application/json; charset=utf-8");

    private XTHttpUtil() {
        mRandom = new Random(System.currentTimeMillis());
    }

    public synchronized static XTHttpUtil getInstance() {
        if (XT_HTTP == null) {
            XT_HTTP = new XTHttpUtil();
        }
        return XT_HTTP;
    }


    /**
     * post 请求
     *
     * @param callBack      回调
     * @param url           url地址
     * @param singleRequest 是否单一请求
     * @param param         参数
     * @param tag           tag
     * @return 请求是否发送成功
     */
    public boolean doPost(IRequestCallback<ResultModel> callBack, String url, boolean singleRequest, Map<String, Object> param, Object tag) {
        XTParamCommom commomParam = new XTParamCommom();
        Map<String, Object> commomParamMap = commomParam.build();
        if (!singleRequest) {
            commomParamMap.put("random", mRandom.nextInt());
        }

//        String token = UserInfoManager.getManager(LovePetApplication.getInstance()).getToken();
        String body = JSON.toJSONString(param);
        L.d(TAG, "body=" + body);
//        String md5Str = SecretUtil.getSign(token, getValue(commomParamMap), body, UrlConstant.IS_DEBUG);
//        commomParamMap.put("sign", md5Str);
        String requestUrl = StringUtil.urlJoint(url, commomParamMap);
        RequestBody requestBody = RequestBody.create(MediaType_JSON, body);
        return post(callBack, requestUrl, requestBody, null, tag);
    }

    /**
     * post 请求
     *
     * @param callBack 回调
     * @param url      url地址
     * @param param    参数
     * @param tag      tag
     * @return 请求是否发送成功
     */
    public boolean doPost(IRequestCallback<ResultModel> callBack, String url, Map<String, Object> param, Object tag) {
        return doPost(callBack, url, true, param, tag);
    }

    /**
     * get请求
     *
     * @param callBack      回调
     * @param url           url地址
     * @param singleRequest 是否单一请求
     * @param param         参数
     * @param tag           tag
     * @return 请求是否发送成功
     */
    public boolean doGet(IRequestCallback<ResultModel> callBack, String url, boolean singleRequest, Map<String, Object> param, Object tag) {
        XTParamCommom commomParam = new XTParamCommom();
        Map<String, Object> commomParamMap = commomParam.build();
        if (param != null && param.size() > 0) {
            commomParamMap.putAll(param);
        }
        if (!singleRequest) {
            commomParamMap.put("random", mRandom.nextInt());
        }
//        String token = UserInfoManager.getManager(LovePetApplication.getInstance()).getToken();
//        String md5Str = SecretUtil.getSign(token, getValue(commomParamMap), null, UrlConstant.IS_DEBUG);
//        commomParamMap.put("sign", md5Str);
        String requestUrl = StringUtil.urlJoint(url, commomParamMap);
        L.d(TAG, "requestUrl=" + requestUrl);
        return get(callBack, requestUrl, null, tag);
    }

    /**
     * get 请求
     *
     * @param callBack 回调
     * @param url      url地址
     * @param param    参数
     * @param tag      tag
     * @return 请求是否发送成功
     */
    public boolean doGet(IRequestCallback<ResultModel> callBack, String url, Map<String, Object> param, Object tag) {
        return doGet(callBack, url, true, param, tag);
    }


    public static String getValue(Map<String, Object> param) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> m : param.entrySet()) {
            if (!"token".equals(m.getKey())) {
                sb.append(m.getKey());
                sb.append(m.getValue());
            }
        }
        return sb.toString();
    }
}
